﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using MINERP.DAL;
using MINERP.DAL.CustomerManage;
using MINERP.COM;

namespace MINERP.BLL.CustomerManage
{
    public class WorkOrderFlowListManageBLL : CustomerManagerBLLBase
    {

        #region //Private fields

        private List<WorkOrderFlowList>                     _collection = null;
        private List<WorkOrderFlowList_BK>                  _collection_bk = null;             //流程备份

        private List<WorkOrderFlowListEntity>               _resultList = new List<WorkOrderFlowListEntity>();


        /// <summary>
        /// 所有的流程节点(缓存起来)
        /// </summary>
        private List<TechInfo> _resultTechList = new List<TechInfo>();
        

        #endregion

        #region //Public Methods

        /// <summary>
        /// 通过订单号
        /// </summary>
        /// <param name="orderBagid"></param>
        /// <returns></returns>
        public List<WorkOrderFlowListEntity> GetWorkerFlowListByOrderBag(int orderBagid)
        {
            _collection = WorkerOrderFlowListDAL.GetOrderFlowListByBagID(orderBagid);

            if (_resultList.Count > 0)
                _resultList.Clear();

            foreach (WorkOrderFlowList item in _collection)
            {
                _resultList.Add(new WorkOrderFlowListEntity(item));
            }

            return _resultList;
        }

        /// <summary>
        /// 通过产品ID得到产品的流程
        /// </summary>
        /// <param name="infoid"></param>
        /// <returns></returns>
        public List<WorkOrderFlowListEntity> GetWorkerFlowListByOrderInfo(int infoid)
        {
            _collection = WorkerOrderFlowListDAL.GetOrderFlowListByOrderInfoID(infoid);

            if (_resultList.Count > 0)
                _resultList.Clear();

            foreach (WorkOrderFlowList item in _collection)
            {
                _resultList.Add(new WorkOrderFlowListEntity(item));
            }

            return _resultList;
        }
        /// <summary>
        /// 通过产品ID得到产品的流程(备份)
        /// </summary>
        /// <param name="infoid"></param>
        /// <returns></returns>
        public List<WorkOrderFlowListEntity> GetWorkerFlowListByOrderInfo_bk(int infoid)
        {
            _collection_bk = WorkerOrderFlowListDAL.GetOrderFlowListByOrderInfoID_bk(infoid);

            if (_resultList.Count > 0)
                _resultList.Clear();

            foreach (WorkOrderFlowList_BK item in _collection_bk)
            {
                _resultList.Add(new WorkOrderFlowListEntity(item));
            }

            return _resultList;
        }

        /// <summary>
        /// 得到产品的生产状态
        /// </summary>
        /// <param name="infoid"></param>
        /// <returns></returns>
        public bool GetOrderInfoStats(int infoid)
        {
            return WorkerOrderFlowListDAL.GetOrderInfoStatusByOrderInfo(infoid);
        }

        /// <summary>
        /// 得到所有的用户信息用于绑定
        /// </summary>
        /// <returns></returns>
        public List<UserInfo> GetUserInfo()
        {
            return UserDAL.GetUserInfo();
        }

        /// <summary>
        /// 得到节点信息进行绑定
        /// </summary>
        /// <returns></returns>
        public void GetAllTechName()
        {
            _resultTechList = WorkerOrderFlowListDAL.GetAllTechName();
        }

        #endregion

    }

    #region //Entity class

        public class WorkOrderFlowListEntity
        {

            public bool IsDirty = false;


            public WorkOrderFlowListEntity()
            {

            }


            public WorkOrderFlowListEntity(WorkOrderFlowList obj)
            {
                if (obj.tb_Tech != null)
                {
                    TechName = obj.tb_Tech.TechName;
                    WorkOrder = obj.tb_Tech.WokerOrder;
                }
                if (obj.UserId == 0)
                {
                    UserName = "未分配";
                }
                else
                {
                    UserName = WorkerOrderFlowListDAL.GetUserNameByUserID(obj.UserId);
                }
                if (obj.AssignTime != null)
                    AssignTime = obj.AssignTime;
                if (obj.CompletedTime != null)
                    CompletedTime = obj.CompletedTime;
                if (obj.PreCompleteTime != null)
                    PreCompleteTime = obj.PreCompleteTime;
                OrderFlowListStatus status = (OrderFlowListStatus)obj.Status;
                switch (status)
                {
                    case OrderFlowListStatus.Wait:
                        Status = "等待"; break;
                    case OrderFlowListStatus.Useable:
                        Status = "等待接收"; break;
                    case OrderFlowListStatus.UseableAccept:
                        Status = "组员已接收"; break;
                    case OrderFlowListStatus.Error:
                        Status = "错误"; break;
                    case OrderFlowListStatus.Complete:
                        Status = "已完成"; break;
                    case OrderFlowListStatus.Disable:
                        Status = "不可用"; break;
                    case OrderFlowListStatus.UnProduct:
                        Status = "停止生产"; break;
                    default: Status = "未知"; break;
                }
                StatusValue = obj.Status;
                IsEdit = obj.IsEdit;
            }
            public WorkOrderFlowListEntity(WorkOrderFlowList_BK obj)
            {
                if (obj.tb_Tech != null)
                {
                    TechName = obj.tb_Tech.TechName;
                    WorkOrder = obj.tb_Tech.WokerOrder;
                }
                if (obj.UserId == 0)
                {
                    UserName = "未分配";
                }
                else
                {
                    UserName = WorkerOrderFlowListDAL.GetUserNameByUserID(obj.UserId);
                }
                if (obj.AssignTime != null)
                    AssignTime = obj.AssignTime;
                if (obj.CompletedTime != null)
                    CompletedTime = obj.CompletedTime;
                if (obj.PreCompleteTime != null)
                    PreCompleteTime = obj.PreCompleteTime;
                OrderFlowListStatus status = (OrderFlowListStatus)obj.Status;
                switch (status)
                {
                    case OrderFlowListStatus.Wait:
                        Status = "等待"; break;
                    case OrderFlowListStatus.Useable:
                        Status = "等待接收"; break;
                    case OrderFlowListStatus.UseableAccept:
                        Status = "组员已接收"; break;
                    case OrderFlowListStatus.Error:
                        Status = "错误";break;
                    case OrderFlowListStatus.Complete:
                        Status = "已完成"; break;
                    case OrderFlowListStatus.Disable:
                        Status = "不可用"; break;
                    case OrderFlowListStatus.UnProduct:
                        Status = "停止生产"; break;
                    default: Status = "未知"; break;
                }
                StatusValue = obj.Status;
                IsEdit = obj.IsEdit;
            }
            public string TechName
            {
                get;
                set;
            }

            public int WorkOrder
            {
                get;
                set;
            }

            public string UserName
            {
                get;
                set;
            }

            public Nullable<DateTime> AssignTime
            {
                get;
                set;

            }


            public Nullable<DateTime> CompletedTime
            {
                get;
                set;
            }

            public Nullable<DateTime> PreCompleteTime
            {
                get;
                set;
            }

            public string Status
            {
                get;
                set;
            }

            public int StatusValue
            {
                get;
                set;
            }

            /// <summary>
            /// 是否过期
            /// </summary>
            public string IsTimeOut
            {
                get;
                set;
            }

            public bool IsEdit
            {
                get;
                set;
            }

            /*
            public string TechName
            {
                get
                {
                    return this._obj.tb_Tech.TechName;
                }

            }

            public int WorkOrder
            {
                get
                {
                    return this._obj.WorkOrder;
                }
                set
                {
                    if (this._obj.WorkOrder != value)
                    {
                        this.IsDirty = true;
                        this._obj.WorkOrder = value;
                    }
                }
            }

            public string UserName
            {
                get
                {
                    if (this._obj.UserId == 0)
                    {
                        return "未分配";
                    }

                    return WorkerOrderFlowListDAL.GetUserNameByUserID(this._obj.UserId);
                }
                set
                {
                    int userid = WorkerOrderFlowListDAL.GetUserIDByUserName(value);
                    if (this._obj.UserId != userid)
                    {
                        this.IsDirty = true;
                        this._obj.UserId = userid;
                    }
                }
            }

            public Nullable<DateTime> AssignTime
            {
                get
                {
                     return this._obj.AssignTime;
                }

            }


            public Nullable<DateTime> CompletedTime
            {
                get
                {
                    if (this._obj.Status == (int)OrderFlowListStatus.Complete)
                    {
                        return this._obj.CompletedTime;
                    }

                    
                    return null;
                }
                set
                {
                    if (this._obj.CompletedTime != value)
                    {
                        this.IsDirty = true;
                        this._obj.CompletedTime = value;
                    }
                }
            }

            public Nullable<DateTime> PreCompleteTime
            {
                get
                {
                    if (this._obj.Status != (int)OrderFlowListStatus.Complete)
                    {
                        return this._obj.PreCompleteTime;
                    }

                    return null;
                }
            }

            public string Status
            {
                get
                {
                    OrderFlowListStatus status = (OrderFlowListStatus)this._obj.Status;
                    switch (status)
                    { 
                        case OrderFlowListStatus.Wait:
                            return "等待";
                        case OrderFlowListStatus.Useable:
                            return "等待接收";
                        case OrderFlowListStatus.UseableAccept:
                            return "组员已接收";
                        case OrderFlowListStatus.Error:
                            return "错误";
                        case OrderFlowListStatus.Complete:
                            return "已完成";
                        case OrderFlowListStatus.Disable:
                            return "不可用";
                        case OrderFlowListStatus.UnProduct:
                            return "停止生产";

                    }

                    return "未知";
                }
            }

            public int StatusValue
            {
                get
                {
                    return this._obj.Status;
                }
                set
                {
                    this._obj.Status = value;
                }
            }

            /// <summary>
            /// 是否过期
            /// </summary>
            public string IsTimeOut
            {
                get
                {
                    if (this._obj.Status != (int)OrderFlowListStatus.Complete)
                    {
                        if (this._obj.PreCompleteTime <= DateTime.Now.AddHours(TimeCosntVar.TimeOutHour))
                        {
                            return "已经超期";
                        }
                    }

                    return "正常生产";
                }
            }

            public bool IsEdit
            {
                get
                {
                    return this._obj.IsEdit;
                }
            }*/
        }

        #endregion
}
